<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Www_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
    }

    /**
     * 首页
     */
    public function index()
    {
        // 广告图
        $this->load->model('banner_m');
        $banner = $this->banner_m->select(['type' => 0],3);
        if ($banner) {
            foreach ($banner as $key => $value) {
                $banner[$key]['pic_url'] = str_replace('uploads/', "/uploads/", $value['pic_url']);
                $banner[$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner[$key]['pic_url']);
            }
        }
        $data['banner'] = $banner;
        // 轮播图
        $banner2 = $this->banner_m->select(['type' => 1]);
        if ($banner2) {
            foreach ($banner2 as $key => $value) {
                $banner2[$key]['pic_url'] = str_replace('uploads/', "/uploads/", $value['pic_url']);
                $banner2[$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner2[$key]['pic_url']);
            }
        }
        $data['banner2'] = $banner2;
        // 最新
        $this->load->model('account_m');
        $hot         = $this->account_m->select(['deletetd' => 0], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['hot'] = $hot;
        // 置顶
        $up         = $this->account_m->findBy(['deletetd' => 0, "is_hot" => 1], "id,title,describe");
        $data['up'] = $up;
        // 推荐banner
        $recommend         = $this->account_m->findBy(['deletetd' => 0, "is_recommend" => 1], "id,title,img");
        $data['recommend'] = $recommend;
        // 平安广州
        $pingan         = $this->account_m->select(['deletetd' => 0, "category" => 2], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['pingan'] = $pingan;
        // 法治广州
        $fazhi         = $this->account_m->select(['deletetd' => 0, "category" => 3], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['fazhi'] = $fazhi;
        // 队伍建设
        $duiwu         = $this->account_m->select(['deletetd' => 0, "category" => 5], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['duiwu'] = $duiwu;
        // 基层创新
        $jiceng         = $this->account_m->select(['deletetd' => 0, "category" => 8], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['jiceng'] = $jiceng;
        // 他山之石
        $tashan         = $this->account_m->select(['deletetd' => 0, "category" => 9], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['tashan'] = $tashan;
        // 高层声音
        $gaoceng         = $this->account_m->select(['deletetd' => 0, "category" => 10], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['gaoceng'] = $gaoceng;
        // 共建共治共享
        $gongjian         = $this->account_m->select(['deletetd' => 0, "category" => 4], 6, 0, "id,title,keyword,describe,create_time,img");
        $data['gongjian'] = $gongjian;
        // 政法时评
        $zhengfa         = $this->account_m->select(['deletetd' => 0, "category" => 11], 7, 0, "id,title,keyword,describe,create_time,img");
        $data['zhengfa'] = $zhengfa;
        // 政法视频
        $video         = $this->account_m->select(['deletetd' => 0, "category" => 12], 7, 0, "id,title,keyword,describe,create_time,img");
        $data['video'] = $video;
      // 政法头条
         $toutiao         = $this->account_m->select(['deletetd' => 0, "category" => 13], 10, 0, "id,title,keyword,describe,create_time,img");
        $data['toutiao'] = $toutiao;
        // 机关动态
        $jiguan         = $this->account_m->select(['deletetd' => 0, "category" => 14], 7, 0, "id,title,keyword,describe,create_time,img");
        $data['jiguan'] = $jiguan;
        // 机关动态
        $lawer         = $this->account_m->select(['deletetd' => 0, "category" => 15], 7, 0, "id,title,keyword,describe,create_time,img");
        $data['lawer'] = $lawer;
        // 社会百态
        $baitai         = $this->account_m->select(['deletetd' => 0, "category" => 16], 7, 0, "id,title,keyword,describe,create_time,img");
        $data['baitai'] = $baitai;
        // pp($up);
        // exit();
        $this->load->view('index', $data);
    }

    /**
     * 直通资讯
     */
    public function news($id = 1, $page = 1)
    {

        switch ($id) {
            default:
                $t = "safe";
                break;
        }

        $where = ['deletetd' => 0];
        if ($id) {
            $where['category'] = $id;
        }
        $this->load->model('account_m');
        $this->load->model('zixunCategory_m');
        $this->load->library('pagination');
        $limit                      = 20;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url($t);
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,category,title,describe,create_time,img";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];
        $data["id"]         = $id;

        $data['category'] = $this->zixunCategory_m->findBy(['id' => $id]);

        $this->load->model('banner_m');
        $banner = $this->banner_m->findBy(['type' => 0]);
        if ($banner) {
            $banner['pic_url'] = str_replace('uploads/', "/uploads/", $banner['pic_url']);
            $banner['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pic_url']);
        }
        // 置顶
        $up                = $this->account_m->findBy(['deletetd' => 0, "is_hot" => 1], "id,title,describe");
        $data['up']        = $up;
        $data['banner']    = $banner;
        $data['menu_name'] = $t;

        $this->load->view("news", $data);
    }

    /**
     * 资讯详情
     */
    public function news_show($id = 0)
    {
        $this->load->model('account_m');
        $article         = $this->account_m->find($id);
        $data["article"] = $article ?? [];
        // 分类

        $data['category'] = $this->zixunCategory_m->find($article['category']);

        switch ($article['category']) {
            default:
                $t = "safe";
                break;
        }

        $this->load->model('banner_m');
        $banner = $this->banner_m->findBy(['type' => 0]);
        if ($banner) {
            $banner['pic_url'] = str_replace('uploads/', "/uploads/", $banner['pic_url']);
            $banner['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pic_url']);
        }
        // 置顶
        $up                = $this->account_m->findBy(['deletetd' => 0, "is_hot" => 1], "id,title");
        $data['up']        = $up;
        $data['banner']    = $banner;
        $data['menu_name'] = $t;
        // pp($data);exit();
        $this->load->view('news_details', $data);
    }

    /**
     * 柔美
     */
    public function page($id = 1)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['rid']  = $id;

        $this->load->view('about', $data);
    }

    /**
     * 关于我们
     */
    public function contact()
    {
        $data['rid'] = 5;
        $this->load->view('contact', $data);
    }

    public function jobs()
    {
        $data['rid'] = 5;
        $this->load->view('jobs', $data);
    }

    /**
     * 加入我们
     */
    public function join($id = 5)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['id']   = $id;
        $this->load->view('join', $data);
    }

    /**
     * 皮肤护理
     */
    public function product($id = 1, $page = 1)
    {

        $this->load->model('article_m');
        $where = [];
        if ($id) {
            $where = ['category' => $id];
        }
        //分页
        $limit                      = 6;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->article_m->total($where);
        $config['base_url']         = base_url('product/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $products           = $this->article_m->select($where, $limit, $start);

        $data["products"] = $products ?? [];
        $data["id"]       = $id;
        // pp($data);exit();
        $this->load->view('product', $data);
    }

    /**
     * 资讯详情
     */
    public function product_show($id = 0)
    {
        $this->load->model('article_m');
        $this->load->model('articleCategory_m');
        $article         = $this->article_m->find($id);
        $data["article"] = $article ?? [];
        // 分类
        $cur_category         = $this->articleCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        //获取上一篇
        // $data["pre_article"]  = $this->account_m->findBy("id < {$id}");
        // $data['next_article'] = $this->account_m->findBy("id > {$id}");
        // pp($data);exit();
        $this->load->view('product_show', $data);
    }

    /**
     * 合作流程
     */
    public function price()
    {
        $this->load->view('price');
    }

    /**
     * 留言提交
     */
    public function message_add()
    {

        $post    = $this->input->post();
        $title   = isset($post['name']) ? htmlspecialchars($post['name'], ENT_QUOTES) : false;
        $address = isset($post['address']) ? htmlspecialchars($post['address'], ENT_QUOTES) : false;
        $tel     = isset($post['mobile']) ? htmlspecialchars($post['mobile'], ENT_QUOTES) : false;
        $email   = isset($post['email']) ? htmlspecialchars($post['email'], ENT_QUOTES) : false;
        $con     = isset($post['content']) ? htmlspecialchars($post['content'], ENT_QUOTES) : false;

        if (!$title) {
            echo json_encode(['status' => 0, 'msg' => '姓名不能为空']);
            exit();
        }
        if (!$tel) {
            echo json_encode(['status' => 0, 'msg' => '电话不能为空']);
            exit();
        }
        if (!$email) {
            echo json_encode(['status' => 0, 'msg' => '邮箱不能为空']);
            exit();
        }
        if (!$con) {
            echo json_encode(['status' => 0, 'msg' => '备注不能为空']);
            exit();
        }
        $properties = [
            'name'    => $title,
            'tel'     => $tel,
            'email'   => $email,
            'con'     => $con,
            'address' => $address,
            'time'    => date("Y-m-d H:i:s", time()),
        ];
        // 新增
        $this->load->model('message_m');

        $properties = $this->message_m->insert($properties);
        if ($properties) {
            echo json_encode(['status' => 1, 'msg' => '申请成功']);
            exit();
        } else {
            echo json_encode(['status' => 0, 'msg' => '申请失败']);
            exit();
        }
    }

    public function search($page = 1)
    {
        $key = $this->input->get("key");

        $where = "deletetd = 0";
        if ($key) {
            $where .= " and title like '%{$key}%'";
        }
        $this->load->model('account_m');
        $this->load->model('zixunCategory_m');
        $this->load->library('pagination');
        $limit                      = 20;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url("search");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,category,title,describe,create_time,img";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];

        $this->load->model('banner_m');
        $banner = $this->banner_m->findBy(['type' => 0]);
        if ($banner) {
            $banner['pic_url'] = str_replace('uploads/', "/uploads/", $banner['pic_url']);
            $banner['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pic_url']);
        }
        // 置顶
        $up              = $this->account_m->findBy(['deletetd' => 0, "is_hot" => 1], "id,title");
        $data['up']      = $up;
        $data['banner']  = $banner;
        $data['keyword'] = $key;

        // pp($data);exit();
        $this->load->view("search", $data);
    }

}
